<?php
/**
 * 类库文件
 * @author Monkee
 * @version zhuopang v1.0
 * @copyright (c) 2011, Monkee
 */

/**
 * 图片处理类
 *
 * @author monkee
 *
 */
class imageModel extends model
{
	private $file;
	private $width;
	private $height;
	private $res;
	/** 
	 * @param string $file 源图片文件文件
	 */
	function newimage($file){
		$this->file = trim($file);
		list($this->width, $this->height, $type) = @getimagesize($this->file);
		$types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
		if(in_array($type, array(1, 2, 3))){
			$func = 'imagecreatefrom'.$types[$type];
			$this->res = $func($this->file);
		}
	}
	/**
	 * 更改图片大小
	 * 
	 * @param int $width 更改后的图片宽度
	 * @param string $desc 更改后的文件存储地址
	 */
	public function changeSize($width, $desc){
		if(empty($this->res)){
			return false;
		}
		$height = (int)($width / $this->width * $this->height);
		$dres = imagecreatetruecolor($width, $height);
		$result = imagecopyresized($dres, $this->res, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
		if($result === false){
			return false;
		}
		return imagejpeg($dres, $desc);
	}
	
}
?>